package com.woniu.idearbac.Controller;

import com.woniu.idearbac.Service.RolesService;
import com.woniu.idearbac.dto.RolesDTO;
import com.woniu.idearbac.entity.SysRole;
import com.woniu.idearbac.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/11/30-时间-13:27
 * @product idea-rbac
 */
@RestController
@RequestMapping("/roles")
public class RolesController {
    @Resource
    private RolesService rolesService;

    @GetMapping("")
    public Result selAll() {
        List<SysRole> roles = rolesService.selAll();
        return Result.ok().data("roleList", roles);
    }

    @GetMapping("{uid}")
    public Result getRole(@PathVariable Long uid) {
        List<SysRole> roles = rolesService.selAll();
        List<Integer> list = rolesService.selRolesByUid(uid);
        return Result.ok().data("roleList", roles).data("checkList", list);
    }

    @PostMapping("")
    public Result update(@RequestBody RolesDTO rolesDTO) {
        int i = rolesService.insertRoles(rolesDTO);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysRole sysRole) {
        int i = rolesService.insertSelective(sysRole);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @PostMapping("/selOne")
    public Result selOne(@RequestBody SysRole sysRole) {
        Long id = Long.valueOf(sysRole.getId());
        SysRole role = rolesService.selectByPrimaryKey(id);
        return Result.ok().data("list", role);
    }

    @PutMapping("")
    public Result update(@RequestBody SysRole sysRole) {
        int i = rolesService.updateByPrimaryKeySelective(sysRole);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id) {
        int i = rolesService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}
